//
//  MyProfileViewController.m
//  jungoo
//
//  Created by Dang Quang Trung on 7/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MyProfileViewController.h"
#import "AppDelegate.h"
#import "User.h"

#import "Request.h"
#import "Course.h"
#import "Module.h"
#import "College.h"
#import "user_image.h"
#import "SettingViewController.h"
#import "RelationshipViewControlle.h"
#import "ModuleViewController.h"
#import "CourseViewController.h"
#import "SchoolViewController.h"
#import "ImageButtonView.h"
#import "Extensions.h"

#import "CreateAccountMoreController.h"

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 5.0f

@implementation MyProfileViewController

@synthesize tableView1;
@synthesize tableView2;
@synthesize avatar;
@synthesize profileCell;
@synthesize albumVC;
@synthesize relationshipCell;
@synthesize avatarCell;
@synthesize myProfileMode = _myProfileMode;
@synthesize profileForViewMode = _profileForViewMode;
@synthesize selectedIndexPath;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];    
}

#pragma mark - Private Method

- (void)showYearView:(BOOL)bShow
{
    if (bShow) {
        CGRect yvRect = CGRectMake(0, 460, 320, 460);        
        
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        [self.navigationController.view addSubview:yearUI];
        yearUI.frame = yvRect;
        yvRect = CGRectMake(0, 20, 320, 416);
        yearUI.frame = yvRect;
        [UIView commitAnimations];
        
        self.view.userInteractionEnabled = NO;
    }
    else {        
        self.view.userInteractionEnabled = YES;
        
        [UIView animateWithDuration:0.3 
                         animations:^{
                             CGRect dobRect = CGRectMake(0, 460, 320, 460);
                             yearUI.frame = dobRect;                               
                         }
                         completion:^(BOOL finished){    
                             [yearUI removeFromSuperview];
                         }];                
    }
}

- (void)asyncAutoSave
{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    
    dispatch_async(queue, ^{
        [self performSelector:@selector(delaySave:) withObject:nil];
    });       
}

#pragma mark - View lifecycle

-(void)applyAppearance 
{
    if (self.myProfileMode == EditMode) {
        [self setBackButtonImageActiveName:nil 
                              inactiveName:@"btn_home.png" 
                                    target:self 
                                    action:@selector(backPressed)];
    }
    else {
        [self setBackButtonImageActiveName:nil 
                              inactiveName:@"back-button" 
                                    target:self 
                                    action:@selector(backPressed)];
    }    
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"home_bg.png"]]]; 
}

-(void) backPressed 
{
    [self asyncAutoSave];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)editAvatar:(id)sender
{
    if (self.myProfileMode == ViewMode) {
        return;
    }
    albumVC.navController = self.navigationController;
    albumVC.photos = listImage;
    albumVC.type = AlbumForAvatar;
    [self.navigationController pushViewController:albumVC animated:YES];
}

- (IBAction)doneYearView:(id)sender
{
    YPickerView *datePicker = (id)[[yearUI viewWithTag:1] viewWithTag:11];
    [[JGoUtility sharedCreateAccount] setValue:datePicker.year forKey:@"u_year"];
    [self showYearView:NO];
    [self.tableView2 reloadData];    
}

- (void)save:(id)sender
{
    [JGoUtility showProgressInView:self.view 
                            target:self 
                             title:@"Saving Your Profile..." 
                          selector:@selector(delaySave:)];
}

- (void)loadData:(MBProgressHUD*)progess
{         
    NSError *error = nil;
    NSArray *photos = nil;
    NSDictionary *userInfo = [JGoUtility sharedCreateAccount];
    
    photos = [JGoAPI getPhotoWithUserId:[userInfo objectForKey:@"user_id"] error:&error];
    listImage = (id)[photos retain];
    
    if ([[JGoUtility sharedCreateAccount] objectForKey:@"avatar"] == nil) {
        personAvatar = [JGoAPI downloadAvatar:[userInfo objectForKey:@"thumb"] error:&error];        
        [personAvatar retain];
        [userInfo setValue:personAvatar forKey:@"avatar"];
    }
    else {
        personAvatar = [[JGoUtility sharedCreateAccount] objectForKey:@"avatar"];
    }        
    
    [[AppDelegate shareDelegate] hideLoadingView];
    
    [tableView2 reloadData];
    
    [progess hide:YES];
}

- (void)viewDidLoad
{    
    [super viewDidLoad];
    [self.tableView2 setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView2.backgroundColor = [UIColor clearColor];
    self.tableView2.opaque = NO;
    self.tableView2.backgroundView = nil;
    
     
    [JGoUtility showProgressInView:self.view 
                            target:self 
                             title:@"Loading Data.."
                          selector:@selector(loadData:)];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(notificationReloadAvatar:) 
                                                 name:JGO_PROFILE_RELOAD_AVATAR object:nil];
    
    
    YPickerView *yearPicker = (id)[[yearUI viewWithTag:1] viewWithTag:11];
    yearPicker.year = [[JGoUtility sharedCreateAccount] objectForKey:@"u_year"];
}


-(void)viewWillAppear:(BOOL)animated
{
    [self applyAppearance];    
    [self setTitle:@"Profile"];
    [super viewWillAppear:animated];
    if (isRefresh) {
        [JGoUtility showProgressInView:self.view
                                target:self
                                 title:@"Loading Data.."
                              selector:@selector(loadData:)];
    }
    
    [tableView2 reloadData];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

-(void)dealloc
{
    [albumVC release];
    [avatar release];
    [tableView1 release];
    [tableView2 release];
    [profileCell release];
    [backupUserInfo release];
    [yearUI release];
    [selectedIndexPath release];
    
    [super dealloc];
}

#pragma mark - Notification

- (void)notificationReloadAvatar:(NSNotification*)notice
{
    NSDictionary *userInfor = notice.userInfo;
    
    if (userInfor == nil) {
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
        
        dispatch_async(queue, ^{
            
            NSDictionary *userInfo = [JGoUtility sharedCreateAccount];
            NSError *error = nil;
            personAvatar = [JGoAPI downloadAvatar:[userInfo objectForKey:@"thumb"] error:&error];
            [personAvatar retain];
            
            if (error) {
                LOG(@"%@",[error localizedDescription]);
            }
            else {
                
               [self.tableView2 performSelectorOnMainThread:@selector(reloadData) 
                                                 withObject:nil waitUntilDone:YES];
                
                
            }
        });        
    }
    else {
        personAvatar = [userInfor objectForKey:@"avatar"];
        [self.tableView2 reloadData];
    }
}

#pragma mark - Table Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{    
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   if (section == 3) {
       return 4;
   }  
   else {      
      return 1;
   }        
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    
    switch (indexPath.section) {
        case 0:
        {
            return 74;
        }
        case 3:
        {
                        
            if (indexPath.row == 2) {
                
                NSString *text = [JGoUtility stringFromArray:[[JGoUtility sharedCreateAccount] objectForKey:@"modules"]];
                NSLog(@"%@",text);
                CGSize constraint = CGSizeMake(180, 20000.0f);
                
                CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
                
                CGFloat height = MAX(size.height, 44.0f);
                
                return height + (CELL_CONTENT_MARGIN * 2);
            }else if (indexPath.row ==3)
            {
                NSString *text = [[JGoUtility sharedCreateAccount] objectForKey:@"college"];
                
                CGSize constraint = CGSizeMake(180, 20000.0f);
                
                CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
                
                CGFloat height = MAX(size.height, 44.0f);
                
                return height + (CELL_CONTENT_MARGIN * 2);

            }else
            {
                return 44;
            }
            break;
        }
        default:
        {
            return 44;
            break;
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   if (section == 3) {
       return 22.;
   }
   else {
       return 5;
   }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
        switch (section) {
           
            case 3:
            {
                UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
                lblText.text = @"  Infomation";
                
                return [lblText autorelease];
                break;
            } 
            default:
            {
                
                break;
            }
        }
    return nil;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    NSDictionary *userInfo = (self.myProfileMode == EditMode ? [JGoUtility sharedCreateAccount] : self.profileForViewMode);
    NSDictionary *userSetting = [userInfo objectForKey:@"setting"];    
    
    UITableViewCell *cell;

    if (indexPath.section == 0) {
        
        static NSString *AvatarCellIdentifier = @"AvatarCell";
        cell = [tableView dequeueReusableCellWithIdentifier:AvatarCellIdentifier];
        if (cell == nil) {
            cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:AvatarCellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
            cell = avatarCell;
            self.avatarCell = nil;
        }
        UIButton *icon = (UIButton *)[cell viewWithTag:1];
        UILabel *firstText = (UILabel *)[cell viewWithTag:2];
        UILabel *secondText = (UILabel *)[cell viewWithTag:3];
        UILabel *thirdText = (UILabel *)[cell viewWithTag:4];
       
        if (personAvatar != nil && self.myProfileMode == EditMode) {
            
            [icon setBackgroundImage:personAvatar forState:UIControlStateNormal];
            [icon setImage:[UIImage imageNamed:@"avatar_empty_edit"] forState:UIControlStateNormal];
        }
        else {
            [icon setImage:[userInfo objectForKey:@"avatar"] forState:UIControlStateNormal];
        }
                    
        [icon addTarget:self action:@selector(editAvatar:) forControlEvents:UIControlEventTouchUpInside];
        
        
        firstText.text = [[userInfo objectForKey:@"name"] stringByAppendingFormat:@","];
        firstText.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
        firstText.font = [UIFont systemFontOfSize:14];
        [firstText sizeToFit];
        
        id dob = [userInfo objectForKey:@"dob"];
        
        secondText.text = [JGoUtility stringFromDate4:dob];
        secondText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
        secondText.font = [UIFont systemFontOfSize:14];
        [secondText sizeToFit];
        
        thirdText.font = [UIFont systemFontOfSize:14];
        [thirdText setText:[userInfo objectForKey:@"university"]];
        thirdText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
        cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone; 
        return  cell;
        
    }else if (indexPath.section == 1) {
        
        static NSString *RelationshipCellIdentifier = @"RelationshipCell";
        
        cell = [tableView dequeueReusableCellWithIdentifier:RelationshipCellIdentifier];
        if (cell == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
            cell = relationshipCell;
            UITapGestureRecognizer *tapGuest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeEyes:)] autorelease];
            [[cell imageViewWithTag:1] addGestureRecognizer:tapGuest];
            self.relationshipCell = nil;
        }

    }else {
        static NSString *CellIdentifier = @"ProfileCell";
        
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell  = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            [[NSBundle mainBundle] loadNibNamed:@"ProfileCell" owner:self options:nil];
            cell = profileCell;
            self.profileCell = nil;
        }

    }
            
    UIImageView *icon = (UIImageView *)[cell viewWithTag:1];
    UILabel *firstText = (UILabel *)[cell viewWithTag:2];
    UILabel *secondText = (UILabel *)[cell viewWithTag:3];

    switch (indexPath.section) {
        case 1:
        {
            firstText.text = @"Relationship status:";
            secondText.text = [JGoUtility stringForRelationshipStatus:[[userInfo objectForKey:@"status"] integerValue]];            
            break;
        }
        case 2:
        {
            firstText.text = @"Photos:";
            UILabel *lblNumOfPhoto = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-50, 12, 50, firstText.frame.size.height)];
            lblNumOfPhoto.text = [NSString stringWithFormat:@"%d",[listImage count]];
            lblNumOfPhoto.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
            lblNumOfPhoto.font = [UIFont systemFontOfSize:14];
            [cell addSubview:lblNumOfPhoto];
            [lblNumOfPhoto sizeToFit];
            
            break;
        }
        case 3:
        {
            switch (indexPath.row) {
                case 0:
                {
                    firstText.text = @"Course:";
                    break;
                }
                case 1:
                {
                    firstText.text = @"Class:";
                    break;
                }
                case 2:
                    firstText.text = @"Modules:";
                    break;
                case 3:
                    firstText.text = @"School:";
                    break;
                    
                default:
                    break;
            }
            break;
            
        }
            
        case 4:
            firstText.text = @"Settings";
            break;
        default:
            break;
    }
    firstText.textColor = [UIColor colorWithRed:0. green:0.678 blue:0.941 alpha:1];
    firstText.font = [UIFont systemFontOfSize:14];
    [firstText sizeToFit];
    
    
    CGRect temp = firstText.frame;
    temp.origin.x += temp.size.width+2;
    
    switch (indexPath.section) {
        case 1:
        {
            icon.image = [[userSetting objectForKey:@"status"] intValue] == 1 ? [UIImage imageNamed:@"btn_eye"] : [UIImage imageNamed:@"btn_eye_hide"];
            break;
        }
        case 2:
        {
            secondText.frame = temp;
            icon.image = [UIImage imageNamed:@"btn_photo.png"];
            secondText.text = @"";
            break;
        }
        case 3:
        {
            
            switch (indexPath.row) {
                case 0:
                {
                    temp.size.width = cell.frame.size.width - (firstText.frame.origin.x+firstText.frame.size.width)-2 ;
                    secondText.frame = temp;
                    secondText.text = [NSString stringWithFormat:@"%@ %@",[userInfo objectForKey:@"degree"],[userInfo objectForKey:@"course"]];
                    
                    UITapGestureRecognizer *tapGuest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeEyes:)] autorelease];
                    [[cell imageViewWithTag:1] addGestureRecognizer:tapGuest];
                    
                    icon.image = [[userSetting objectForKey:@"course"] intValue] == 1 ? [UIImage imageNamed:@"btn_eye"] : [UIImage imageNamed:@"btn_eye_hide"];
                    
                    break;
                    
                }
                case 1:
                {
                    temp.size.width = cell.frame.size.width - (firstText.frame.origin.x+firstText.frame.size.width)-2 ;
                    secondText.frame = temp;
                    icon.image = [UIImage imageNamed:@"btn_eye_hide.png"];
                    secondText.text = [userInfo objectForKey:@"u_year"];
                    
                    UITapGestureRecognizer *tapGuest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeEyes:)] autorelease];
                    [[cell imageViewWithTag:1] addGestureRecognizer:tapGuest];
                    
                    icon.image = [[userSetting objectForKey:@"u_year"] intValue] == 1 ? [UIImage imageNamed:@"btn_eye"] : [UIImage imageNamed:@"btn_eye_hide"];
                    
                    [secondText sizeToFit];
                    break;
                }
                case 2:
                {
                    icon.image = [UIImage imageNamed:@"btn_eye.png"];
                    UILabel *label = (UILabel*)[cell viewWithTag:10];
                    if (label == nil) {
                        label = [[UILabel alloc] initWithFrame:CGRectMake(107, 0, 177, 0)];
                        [label setLineBreakMode:UILineBreakModeWordWrap];
                        [label setMinimumFontSize:FONT_SIZE];
                        [label setNumberOfLines:0];
                        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
                        [label setTag:10];
                    }
                    
                    
                    
                    [[cell contentView] addSubview:label];
                    
                    NSArray *modules = [userInfo objectForKey:@"modules"];
                    NSString *text = [JGoUtility stringFromArray:modules];
                    
                    CGSize constraint = CGSizeMake(177, 20000.0f);
                    
                    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (!label)
                        label = (UILabel*)[cell viewWithTag:10];
                    
                    [label setText:text];
                    [label setFrame:CGRectMake(107, 0, 177, MAX(size.height, 44.0f))];
                    secondText.hidden = YES;
                    
                    icon.image = [[userSetting objectForKey:@"modules"] intValue] == 1 ? [UIImage imageNamed:@"btn_eye"] : [UIImage imageNamed:@"btn_eye_hide"];
                    
                    UITapGestureRecognizer *tapGuest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeEyes:)] autorelease];
                    [[cell imageViewWithTag:1] addGestureRecognizer:tapGuest];                                        
                    
                    break;
                }
                case 3:
                {
                    //temp.size.width = cell.frame.size.width - (firstText.frame.origin.x+firstText.frame.size.width)-2 ;
                    //secondText.frame = temp;
                    
                    UILabel *label = (UILabel*)[cell viewWithTag:10];
                    if (label == nil) {
                        label = [[UILabel alloc] initWithFrame:CGRectMake(107, 0, 177, 0)];
                        [label setLineBreakMode:UILineBreakModeWordWrap];
                        [label setMinimumFontSize:FONT_SIZE];
                        [label setNumberOfLines:0];
                        [label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
                        [label setTag:10];
                    }
                    
                    
                    
                    [[cell contentView] addSubview:label];
                    
                    
                    NSString *text = [userInfo objectForKey:@"college"];
                    
                    CGSize constraint = CGSizeMake(177, 20000.0f);
                    
                    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
                    
                    if (!label)
                        label = (UILabel*)[cell viewWithTag:10];
                    
                    [label setText:text];
                    [label setFrame:CGRectMake(107, 0, 177, MAX(size.height, 44.0f))];
                    
                    secondText.hidden = YES;
                    
                    
                    icon.image = [UIImage imageNamed:@"btn_eye.png"];
                    //secondText.text = [userInfo objectForKey:@"college"];
                    
                    icon.image = [[userSetting objectForKey:@"college"] intValue] == 1 ? [UIImage imageNamed:@"btn_eye"] : [UIImage imageNamed:@"btn_eye_hide"];
                    
                    UITapGestureRecognizer *tapGuest = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeEyes:)] autorelease];
                    [[cell imageViewWithTag:1] addGestureRecognizer:tapGuest];
                    break;
                }
                default:
                    break;
            }
            break;
        }
        case 4:
        {
            secondText.frame = temp;
            icon.image = [UIImage imageNamed:@"settings-icon.png"];
            secondText.text = @"";
            break;
            
        }
            
        default:
            
            break;
    }
    secondText.textColor = [UIColor colorWithRed:0.573 green:0.573 blue:0.573 alpha:1];
    secondText.font = [UIFont systemFontOfSize:14];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    
    return cell;    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.myProfileMode == ViewMode) {
        return;
    }
     switch (indexPath.section) {
         case 1:
         {
             RelationshipViewControlle *rVC = [[RelationshipViewControlle alloc] initWithNibName:@"RelationshipViewControlle" 
                                                                                bundle:nil];

             [self.navigationController pushViewController:rVC animated:YES];
             [rVC release];
             break;
         }
         case 2:
         {
             albumVC.navController = self.navigationController;
             albumVC.photos = listImage;
             albumVC.type = AlbumForProfile;
             isRefresh = YES;
             [self.navigationController pushViewController:albumVC animated:YES];
             break;
         }
         case 3:
         {
             switch (indexPath.row) {
                 case 0:
                 {
                     CourseViewController *cVC = [[CourseViewController alloc] initWithNibName:@"CourseViewController" 
                                                                                        bundle:nil];
                    
                     [self.navigationController pushViewController:cVC animated:YES];
                     [cVC release];
                     break;
                 }
                     
                 case 1: {
                     [self showYearView:YES];
                     break;
                 }
                     
                 case 2:
                 {
                     
                     ModuleViewController *mVC = [[ModuleViewController alloc] initWithNibName:@"ModuleViewController" 
                                                                                        bundle:nil];                        
                     [mVC setModules:[[JGoUtility sharedCreateAccount] objectForKey:@"modules"]];
                     [self.navigationController pushViewController:mVC animated:YES];
                     [mVC release];
                     break;
                 }
                 case 3:
                 {
                     SchoolViewController *sVC = [[SchoolViewController alloc] initWithNibName:@"SchoolViewController" 
                                                                                        bundle:nil];
                     
                     [self.navigationController pushViewController:sVC animated:YES];
                     [sVC release];
                     break;
                      
                 }
                 default:
                     break;
             }
             break;
         }
         case 4:
         {
             SettingViewController *sVC = [[SettingViewController alloc] initWithNibName:@"SettingViewController" 
                                                                                bundle:nil];
             [self.navigationController pushViewController:sVC animated:YES];
             [sVC release];
             
             break;
         }
         
         default:
             break;
     }    
}

#pragma mark - Delay Method

- (void)delaySave:(MBProgressHUD*)progress
{
    NSError *error = nil;
    NSString *userId = [[JGoUtility sharedCreateAccount] objectForKey:@"user_id"];
    NSString *modules = [JGoUtility stringFromArray:[[JGoUtility sharedCreateAccount] objectForKey:@"modules"]];
    NSString *dob = [JGoUtility stringFromDate1:[[JGoUtility sharedCreateAccount] objectForKey:@"dob"]];
    dob = dob ? dob : @"0000-00-00";
    modules = modules ? modules : @"";
    
    LOG(@"%@",[[JGoUtility sharedCreateAccount] description]);
    
    [JGoAPI updateProfileWithUserId:userId
                               name:[[JGoUtility sharedCreateAccount] objectForKey:@"name"] 
                            surname:[[JGoUtility sharedCreateAccount] objectForKey:@"surname"] 
                           birthDay:dob 
                             gender:[[JGoUtility sharedCreateAccount] objectForKey:@"gender"] 
                              email:[[JGoUtility sharedCreateAccount] objectForKey:@"email"]  
                             status:[[JGoUtility sharedCreateAccount] objectForKey:@"status"]
                            college:[[JGoUtility sharedCreateAccount] objectForKey:@"college"] 
                         university:[[JGoUtility sharedCreateAccount] objectForKey:@"university"] 
                             course:[[JGoUtility sharedCreateAccount] objectForKey:@"course"] 
                             u_year:[[JGoUtility sharedCreateAccount] objectForKey:@"u_year"] 
                             c_year:[[JGoUtility sharedCreateAccount] objectForKey:@"c_year"] 
                             degree:[[JGoUtility sharedCreateAccount] objectForKey:@"degree"] 
                            modules:modules 
                              thumb:[[JGoUtility sharedCreateAccount] objectForKey:@"thumb"]
                              error:&error];
    if (error) {
        [JGoUtility showAlert:[error localizedDescription]];
    }
    else {
        
        NSDictionary *userSetting = [[JGoUtility sharedCreateAccount] objectForKey:@"setting"];
        
        [JGoAPI saveSettingsWithUserId:userId
                          statusPublic:[[userSetting objectForKey:@"status"] boolValue]
                          coursePublic:[[userSetting objectForKey:@"course"] boolValue] 
                         modulesPublic:[[userSetting objectForKey:@"modules"] boolValue] 
                          u_yearPublic:[[userSetting objectForKey:@"u_year"] boolValue] 
                         collegePublic:[[userSetting objectForKey:@"college"] boolValue] 
                                 error:&error];
        
        if (error) {            
            LOG(@"%@", [error localizedDescription]);
        }
    }
    
    [progress hide:YES];
}

#pragma mark - UITapGestureRecognizer Delegate
-(IBAction)editFullProfile:(id)sender
{
    CreateAccountMoreController * camVC = [[[CreateAccountMoreController alloc] initWithNibName:@"CreateAccountMoreController"
                                                                                         bundle:nil] autorelease];
    camVC.parentVC = self;
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:camVC];
    
    //navController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:navController animated:YES];
}
- (void)changeEyes:(UITapGestureRecognizer*)tapGesture
{
    UIImageView *imageView = (id)tapGesture.view;
    UITableViewCell *cell = (id)[imageView superview].superview;
    NSIndexPath *indexPath = [tableView2 indexPathForCell:cell];
    selectedIndexPath = [indexPath retain];
    NSDictionary *userSetting = [[JGoUtility sharedCreateAccount] objectForKey:@"setting"];
    
    //LOG(@"IndexPath : %d - %d", indexPath.section,indexPath.row);   
    
    switch (indexPath.section) {
        case 1: {    
            
            NSInteger value = [[userSetting objectForKey:@"status"] integerValue];
            if (value) {
                selectedValue = value;
                privateAlertView = [[UIAlertView alloc] initWithTitle:@"Jungoo" 
                                                                    message:@"Are you sure you want to hide this information?" 
                                                                   delegate:self 
                                                          cancelButtonTitle:@"Confirm" 
                                                          otherButtonTitles:@"Cancel", nil];
                [privateAlertView show];
                [privateAlertView release];

            }else {
                selectedValue = value;
                publicAlertView = [[UIAlertView alloc] initWithTitle: @"Jungoo"
                                                              message: @"This information is now public"
                                                             delegate: self
                                                    cancelButtonTitle: @"OK"
                                                    otherButtonTitles: nil];
                [publicAlertView show];
                [publicAlertView release];
                
                

            }
            
            
            break;
        }
            
        case 3: {
            switch (indexPath.row) {
                case 0: {      
                    NSInteger value = [[userSetting objectForKey:@"course"] integerValue];
                    if (value) {
                        selectedValue = value;
                        privateAlertView = [[UIAlertView alloc] initWithTitle:@"Jungoo" 
                                                                            message:@"Are you sure you want to hide this information?" 
                                                                           delegate:self 
                                                                  cancelButtonTitle:@"Confirm" 
                                                                  otherButtonTitles:@"Cancel", nil];
                        [privateAlertView show];
                        [privateAlertView release];
                        
                    }else {
                        selectedValue = value;
                        publicAlertView = [[UIAlertView alloc] initWithTitle: @"Jungoo"
                                                                     message: @"This information is now public"
                                                                    delegate: self
                                                           cancelButtonTitle: @"OK"
                                                           otherButtonTitles: nil];
                        [publicAlertView show];
                        [publicAlertView release];
                        
                    }
                                        
                    break;
                }
                case 1: {   
                    NSInteger value = [[userSetting objectForKey:@"u_year"] integerValue];
                    
                    if (value) {
                        selectedValue = value;
                        privateAlertView = [[UIAlertView alloc] initWithTitle:@"Jungoo" 
                                                                            message:@"Are you sure you want to hide this information?" 
                                                                           delegate:self 
                                                                  cancelButtonTitle:@"Confirm" 
                                                                  otherButtonTitles:@"Cancel", nil];
                        [privateAlertView show];
                        [privateAlertView release];
                        
                    }else {
                        selectedValue = value;
                        publicAlertView = [[UIAlertView alloc] initWithTitle: @"Jungoo"
                                                                     message: @"This information is now public"
                                                                    delegate: self
                                                           cancelButtonTitle: @"OK"
                                                           otherButtonTitles: nil];
                        [publicAlertView show];
                        [publicAlertView release];
                        
                    }
                                        
                    break;
                }
                case 2: {         
                    NSInteger value = [[userSetting objectForKey:@"modules"] integerValue];
                    if (value) {
                        selectedValue = value;
                        privateAlertView = [[UIAlertView alloc] initWithTitle:@"Jungoo" 
                                                                            message:@"Are you sure you want to hide this information?" 
                                                                           delegate:self 
                                                                  cancelButtonTitle:@"Confirm" 
                                                                  otherButtonTitles:@"Cancel", nil];
                        [privateAlertView show];
                        [privateAlertView release];
                        
                    }else {
                        selectedValue = value;
                        publicAlertView = [[UIAlertView alloc] initWithTitle: @"Jungoo"
                                                                     message: @"This information is now public"
                                                                    delegate: self
                                                           cancelButtonTitle: @"OK"
                                                           otherButtonTitles: nil];
                        [publicAlertView show];
                        [publicAlertView release];
                        
                    }
                                       
                    break;
                }
                case 3: {   
                    NSInteger value = [[userSetting objectForKey:@"college"] integerValue];
                    if (value) {
                        selectedValue = value;
                        privateAlertView = [[UIAlertView alloc] initWithTitle:@"Jungoo" 
                                                                            message:@"Are you sure you want to hide this information?" 
                                                                           delegate:self 
                                                                  cancelButtonTitle:@"Confirm" 
                                                                  otherButtonTitles:@"Cancel", nil];
                        [privateAlertView show];
                        [privateAlertView release];
                        
                    }else {
                        selectedValue = value;
                        publicAlertView = [[UIAlertView alloc] initWithTitle: @"Jungoo"
                                                                     message: @"This information is now public"
                                                                    delegate: self
                                                           cancelButtonTitle: @"OK"
                                                           otherButtonTitles: nil];
                        [publicAlertView show];
                        [publicAlertView release];
                        
                    }
                                        
                    break;
                }                    
            }
            break;
        }
    }
    
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSDictionary *userSetting = [[JGoUtility sharedCreateAccount] objectForKey:@"setting"];
    if (alertView == privateAlertView || alertView == publicAlertView) {
        if (buttonIndex == 0)
        {
            switch (self.selectedIndexPath.section) {
                case 1: 
                {
                    
                    selectedValue = !selectedValue;
                    [userSetting setValue:[NSString stringWithFormat:@"%d",selectedValue] forKey:@"status"];
                    LOG(@"%@",[[backupUserInfo objectForKey:@"setting"] description]);
                    
                    [self.tableView2 reloadData];
                    break;
                }
                case 3:
                {
                    switch (self.selectedIndexPath.row) {
                        case 0: 
                        {
                            selectedValue = !selectedValue;
                            [userSetting setValue:[NSString stringWithFormat:@"%d",selectedValue] forKey:@"course"];
                            
                            
                            break;
                        }
                        case 1:
                        {
                            selectedValue = !selectedValue;
                            [userSetting setValue:[NSString stringWithFormat:@"%d",selectedValue] forKey:@"u_year"];
                            
                            
                            break;
                            
                        }
                        case 2:
                        {
                            selectedValue = !selectedValue;
                            [userSetting setValue:[NSString stringWithFormat:@"%d",selectedValue] forKey:@"modules"];
                            break;
                            
                        }
                        case 3:
                        {
                            selectedValue = !selectedValue;
                            [userSetting setValue:[NSString stringWithFormat:@"%d",selectedValue] forKey:@"college"];
                            break;
                        }
                    }
                    [self.tableView2 reloadData];
                    break;
                }
                    
            }
        }
        else
        {  
            //Nothing to do
        }
    }    
    
   
}

@end
